/*
 *	MainWindow.h
 *
 * 	Copyright (C) 2011 Aaron Huang
 * 	This file is part of StackPainter.
 *
 * 	StackPainter is free software: you can redistribute it and/or modify
 * 	it under the terms of the GNU General Public License as published by
 * 	the Free Software Foundation, either version 3 of the License, or
 * 	(at your option) any later version.
 *
 * 	StackPainter is distributed in the hope that it will be useful,
 * 	but WITHOUT ANY WARRANTY; without even the implied warranty of
 * 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * 	GNU General Public License for more details.
 *
 * 	You should have received a copy of the GNU General Public License
 * 	along with StackPainter.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "MainWindow.h"
#include "LineChartWidget.hxx"
#include "IncomeStatementCSVParser.h"
#include "CompanyInfo.h"

#include <QMenu>
#include <QSize>
#include <QAction>
#include <QCloseEvent>
#include <QFileDialog>
#include <QMessageBox>
#include <QString>
#include <QToolBar>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QLabel>
#include <QTextCodec>
#include <QLineEdit>

MainWindow::MainWindow() {
	// Set display attributes.
	const int WINDOW_WIDTH = 800;
	const int WINDOW_HEIGHT = 600;
	resize(QSize(WINDOW_WIDTH, WINDOW_HEIGHT));
	setWindowTitle(tr("Stock"));

	// Initial view attributes.
	createActions();
	createToolbar();
	createViews();

	// Initial model attributes.
	_parser =  new IncomeStatementCSVParser;
}

MainWindow::~MainWindow() {
	delete _parser;
	clearCompanyInfos();
}

void MainWindow::closeEvent(QCloseEvent *event) {
	event->accept();
}

void MainWindow::selectPath() {
	setFunctionWidgetsEnable(false);
	QString path = QFileDialog::getExistingDirectory(this, tr("Open Directory"),
			"", QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
	if (!path.isEmpty()) {
		clearCompanyInfos();
		_parser->setDirPath(path);
		try {
			_companyInfos = _parser->parse();
			_currentCompanyInfo = _companyInfos.begin();
			setFunctionWidgetsEnable(true);
			refreshView();
		} catch (const QString& message) {
			showWarningMessage(message);
		}
	}
}

void MainWindow::showNextCompany() {
	++_currentCompanyInfo;
	if (_currentCompanyInfo == _companyInfos.end()) {
		_currentCompanyInfo = _companyInfos.begin();
	}
	refreshView();
}

void MainWindow::showPreviousCompany() {
	if (_currentCompanyInfo == _companyInfos.begin()) {
		_currentCompanyInfo = _companyInfos.end();
	}
	--_currentCompanyInfo;
	refreshView();
}

void MainWindow::searchCompany() {
	int num = _searchLineEdit->text().toInt();
	bool isFound = false;
	for (QList<CompanyInfo*>::ConstIterator it = _companyInfos.begin();
			it != _companyInfos.end(); ++it) {
		if ((*it)->getNum() == num) {
			_currentCompanyInfo = it;
			refreshView();
			isFound = true;
			break;
		}
	}
	if (!isFound) {
		showWarningMessage("Not such company:" + _searchLineEdit->text());
	}
}

void MainWindow::createActions() {
	_selectPathAct = new QAction(tr("&Select Path"), this);
	connect(_selectPathAct, SIGNAL(triggered()), this, SLOT(selectPath()));

	_exitAct = new QAction(tr("&Exit"), this);
	connect(_exitAct, SIGNAL(triggered()), this, SLOT(close()));
}

void MainWindow::createToolbar() {
	_toolbar = addToolBar(tr("toolbar"));
	_toolbar->setMovable(false);
	_toolbar->addAction(_selectPathAct);
	_toolbar->addAction(_exitAct);
}

void MainWindow::createViews() {
	QWidget* centralWidget = new QWidget(this);
	_vLayout = new QVBoxLayout(centralWidget);
	setCentralWidget(centralWidget);

	_lineChartWidget = new LineChartWidget<double>(this);
	_vLayout->addWidget(_lineChartWidget, 90, 0);

	_hLayout = new QHBoxLayout;
	_vLayout->addLayout(_hLayout);
	_searchLineEdit = new QLineEdit(this);
	_hLayout->addWidget(_searchLineEdit);
	_searchButton = new QPushButton("Search Number", this);
	connect(_searchButton, SIGNAL(clicked()), this, SLOT(searchCompany()));
	_hLayout->addWidget(_searchButton);
	_companyLabel = new QLabel("", this);
	_companyLabel->setAlignment(Qt::AlignCenter);
	_hLayout->addWidget(_companyLabel);
	_previousButton = new QPushButton("Previous", this);
	connect(_previousButton, SIGNAL(clicked()), this, SLOT(showPreviousCompany()));
	_hLayout->addWidget(_previousButton);
	_nextButton = new QPushButton("Next", this);
	connect(_nextButton, SIGNAL(clicked()), this, SLOT(showNextCompany()));
	_hLayout->addWidget(_nextButton);

	setFunctionWidgetsEnable(false);
}

void MainWindow::showWarningMessage(QString message) {
	QMessageBox::warning(this, "Warning", message,
	            QMessageBox::Yes, QMessageBox::Yes);
}

void MainWindow::setCompanyLabel(const CompanyInfo* companyInfo) {
	QTextCodec *codec = QTextCodec::codecForName("UTF-8");
	QString text = QString::number(companyInfo->getNum());
	text = text + " " + companyInfo->getName();
	_companyLabel->setText(codec->toUnicode(text.toAscii()));
}

void MainWindow::refreshView() {
	setCompanyLabel(*_currentCompanyInfo);
	_lineChartWidget->setData((*_currentCompanyInfo)->getEPSs());
	_lineChartWidget->update();
}

void MainWindow::setFunctionWidgetsEnable(bool isEnable) {
	_searchLineEdit->setEnabled(isEnable);
	_searchButton->setEnabled(isEnable);
	_nextButton->setEnabled(isEnable);
	_previousButton->setEnabled(isEnable);
}

void MainWindow::clearCompanyInfos() {
	foreach (CompanyInfo* companyInfo, _companyInfos) {
		delete companyInfo;
	}
	_companyInfos.clear();
}
